<template>
  <!-- 未登录状态：显示完整的未登录首页 -->
  <HomeUnauthenticated v-if="!isAuthenticated" />
  
  <!-- 已登录状态：显示简化版首页 -->
  <div v-else class="home-authenticated">
    <el-card class="welcome-card">
      <h2>欢迎回来，{{ currentUser?.username }}！</h2>
      <p>继续规划您的完美旅程，或探索更多精彩景点。</p>
    </el-card>

    <!-- 推荐景点 -->
    <el-card v-if="recommendations.length > 0" class="recommendations-card">
      <template #header>
        <span>热门推荐</span>
      </template>
      <el-row :gutter="20">
        <el-col :span="6" v-for="rec in recommendations.slice(0, 4)" :key="rec.attraction.id">
          <el-card shadow="hover" class="recommendation-card" @click="viewAttraction(rec.attraction.id)">
            <h3>{{ rec.attraction.name }}</h3>
            <p class="recommendation-reason">{{ rec.reason }}</p>
            <div class="recommendation-info">
              <el-tag type="success" size="small">{{ (rec.score * 100).toFixed(0) }}%</el-tag>
              <span class="price">¥{{ rec.attraction.ticket_price }}</span>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>

    <el-row :gutter="20" class="feature-cards">
      <el-col :span="6">
        <el-card shadow="hover">
          <el-icon><Location /></el-icon>
          <h3>景点管理</h3>
          <p>浏览和管理南昌市各大景点信息</p>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <el-icon><MapLocation /></el-icon>
          <h3>路线规划</h3>
          <p>使用Dijkstra算法规划最优路线</p>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <el-icon><Money /></el-icon>
          <h3>预算管理</h3>
          <p>合理控制旅游预算和费用</p>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <el-icon><DataAnalysis /></el-icon>
          <h3>智能推荐</h3>
          <p>基于多因素权重的智能推荐</p>
        </el-card>
      </el-col>
    </el-row>

    <el-card class="quick-start">
      <h3>快速开始</h3>
      <el-steps :active="0" align-center>
        <el-step title="选择景点" description="选择您想游览的景点" />
        <el-step title="设置预算" description="设置您的旅游预算" />
        <el-step title="规划路线" description="系统自动规划最优路线" />
        <el-step title="查看结果" description="查看详细的路线规划结果" />
      </el-steps>
      <div class="quick-start-button">
        <el-button type="primary" size="large" @click="$router.push('/route-plan')">
          开始规划路线
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { Location, MapLocation, Money, DataAnalysis } from '@element-plus/icons-vue'
import TravelAPI from '../api/travel'
import HomeUnauthenticated from './HomeUnauthenticated.vue'
import logger from '../utils/logger'

export default {
  name: 'Home',
  components: {
    Location,
    MapLocation,
    Money,
    DataAnalysis,
    HomeUnauthenticated
  },
  setup() {
    const router = useRouter()
    const store = useStore()
    const recommendations = ref([])

    const isAuthenticated = computed(() => store.getters.isAuthenticated)
    const currentUser = computed(() => store.getters.currentUser)

    const fetchRecommendations = async () => {
      try {
        const response = await TravelAPI.getRecommendations({
          user_id: currentUser.value?.id,
          limit: 4
        })
        recommendations.value = response.recommendations || []
      } catch (error) {
        logger.error('获取推荐失败:', error)
      }
    }

    const viewAttraction = (id) => {
      router.push(`/attractions?id=${id}`)
    }

    onMounted(() => {
      if (isAuthenticated.value) {
        fetchRecommendations()
      }
    })

    return {
      isAuthenticated,
      currentUser,
      recommendations,
      viewAttraction
    }
  }
}
</script>

<style scoped>
.home-authenticated {
  max-width: 1400px;
  margin: 0 auto;
}

.welcome-card {
  margin-bottom: 30px;
  text-align: center;
  /* 南昌红到滕王阁金渐变 */
  background: linear-gradient(135deg, #C8102E 0%, #D4AF37 100%);
  color: white;
  border: none;
  position: relative;
  overflow: hidden;
}

/* 欢迎卡片装饰元素 */
.welcome-card::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -10%;
  width: 300px;
  height: 300px;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 0%, transparent 70%);
  border-radius: 50%;
  animation: float 20s infinite ease-in-out;
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) scale(1);
  }
  50% {
    transform: translate(-30px, 30px) scale(1.1);
  }
}

.welcome-card :deep(.el-card__body) {
  padding: 40px;
}

.welcome-card h2 {
  color: white;
  margin-bottom: 15px;
  font-size: 32px;
  font-weight: 600;
}

.welcome-card p {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  margin-bottom: 0;
}

.auth-prompt {
  margin-top: 25px;
  display: flex;
  gap: 15px;
  justify-content: center;
}

.auth-prompt .el-button {
  padding: 12px 30px;
  font-size: 16px;
}

.recommendations-card {
  margin-bottom: 30px;
}

.recommendations-card :deep(.el-card__header) {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  padding: 20px;
  border-bottom: 2px solid #f0f0f0;
}

.recommendation-card {
  cursor: pointer;
  transition: all 0.3s;
  height: 100%;
  border-radius: 12px;
  border: 1px solid rgba(200, 16, 46, 0.1);
  background: linear-gradient(135deg, rgba(255,255,255,0.95) 0%, rgba(227,242,253,0.8) 100%);
  backdrop-filter: blur(10px);
}

.recommendation-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 8px 24px rgba(200, 16, 46, 0.2);
  border-color: rgba(212, 175, 55, 0.3);
}

.recommendation-card h3 {
  margin: 0 0 12px 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.recommendation-reason {
  color: #606266;
  font-size: 14px;
  margin: 12px 0;
  line-height: 1.6;
  min-height: 40px;
}

.recommendation-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #f0f0f0;
}

.price {
  font-size: 20px;
  font-weight: bold;
  /* 滕王阁金色 */
  color: #D4AF37;
}

.feature-cards {
  margin-bottom: 30px;
}

.feature-cards .el-card {
  text-align: center;
  height: 220px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  transition: all 0.3s;
  border-radius: 12px;
  border: 1px solid rgba(200, 16, 46, 0.1);
  background: linear-gradient(135deg, rgba(255,255,255,0.95) 0%, rgba(227,242,253,0.8) 100%);
  backdrop-filter: blur(10px);
}

.feature-cards .el-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 8px 24px rgba(200, 16, 46, 0.2);
  border-color: rgba(212, 175, 55, 0.3);
}

.feature-cards .el-icon {
  font-size: 56px;
  /* 南昌红主题色 */
  color: #C8102E;
  margin-bottom: 15px;
  transition: all 0.3s ease;
}

.feature-cards .el-card:hover .el-icon {
  color: #D4AF37;
  transform: scale(1.1) rotate(5deg);
}

.feature-cards h3 {
  margin: 15px 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.feature-cards p {
  color: #909399;
  font-size: 14px;
  line-height: 1.6;
}

.quick-start {
  margin-top: 30px;
  border-radius: 10px;
}

.quick-start h3 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
}

.quick-start-button {
  text-align: center;
  margin-top: 40px;
}

.quick-start-button .el-button {
  padding: 15px 40px;
  font-size: 18px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-card h2 {
    font-size: 24px;
  }
  
  .feature-cards .el-col {
    margin-bottom: 15px;
  }
  
  .recommendation-card {
    margin-bottom: 15px;
  }
}
</style>

